package top.coolrainly.module.pb.controller.admin.user;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import top.coolrainly.framework.common.pojo.PageParam;
import top.coolrainly.framework.common.pojo.PageResult;
import top.coolrainly.framework.common.pojo.CommonResult;
import top.coolrainly.framework.common.util.object.BeanUtils;
import static top.coolrainly.framework.common.pojo.CommonResult.success;

import top.coolrainly.framework.excel.core.util.ExcelUtils;

import top.coolrainly.framework.apilog.core.annotation.ApiAccessLog;
import static top.coolrainly.framework.apilog.core.enums.OperateTypeEnum.*;

import top.coolrainly.module.pb.controller.admin.user.vo.*;
import top.coolrainly.module.pb.dal.dataobject.user.MemberUserDO;
import top.coolrainly.module.pb.service.user.MemberUserService;

@Tag(name = "管理后台 - 用户信息")
@RestController
@RequestMapping("/pb/member-user")
@Validated
public class MemberUserController {

    @Resource
    private MemberUserService memberUserService;

    @PostMapping("/create")
    @Operation(summary = "创建用户信息")
    @PreAuthorize("@ss.hasPermission('pb:member-user:create')")
    public CommonResult<Long> createMemberUser(@Valid @RequestBody MemberUserSaveReqVO createReqVO) {
        return success(memberUserService.createMemberUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户信息")
    @PreAuthorize("@ss.hasPermission('pb:member-user:update')")
    public CommonResult<Boolean> updateMemberUser(@Valid @RequestBody MemberUserSaveReqVO updateReqVO) {
        memberUserService.updateMemberUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pb:member-user:delete')")
    public CommonResult<Boolean> deleteMemberUser(@RequestParam("id") Long id) {
        memberUserService.deleteMemberUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pb:member-user:query')")
    public CommonResult<MemberUserRespVO> getMemberUser(@RequestParam("id") Long id) {
        MemberUserDO memberUser = memberUserService.getMemberUser(id);
        return success(BeanUtils.toBean(memberUser, MemberUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户信息分页")
    @PreAuthorize("@ss.hasPermission('pb:member-user:query')")
    public CommonResult<PageResult<MemberUserRespVO>> getMemberUserPage(@Valid MemberUserPageReqVO pageReqVO) {
        PageResult<MemberUserDO> pageResult = memberUserService.getMemberUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemberUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户信息 Excel")
    @PreAuthorize("@ss.hasPermission('pb:member-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMemberUserExcel(@Valid MemberUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemberUserDO> list = memberUserService.getMemberUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户信息.xls", "数据", MemberUserRespVO.class,
                        BeanUtils.toBean(list, MemberUserRespVO.class));
    }

}